package com.ncu.config;

import com.ncu.filter.JwtAuthenticationTokenFilter;
import com.ncu.service.impl.AccessDeniedHandlerImpl;
import com.ncu.service.impl.AuthenticationEntryPointImpl;
import com.ncu.service.impl.LLSFailureHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import javax.annotation.Resource;


@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig {

    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

    @Autowired
    AuthenticationEntryPointImpl authenticationEntryPoint;
    @Autowired
    AccessDeniedHandlerImpl accessDeniedHandler;

    @Autowired
    LLSFailureHandler failureHandler;

    @Resource
    JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;
    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
                http
                //关闭csrf
                .csrf().disable()
                //不通过Session获取SecurityContext
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeRequests()
                // 对于登录接口 允许匿名访问(认证过了(有token))就不能访问
                .antMatchers("/user/login","/user/captcha","/user/add").permitAll()
                .antMatchers("/user/logout","/file/userAvatar").permitAll()
                        .antMatchers("/realOrderTime/**").permitAll()
                        //静态资源
                        .antMatchers("/foodPricutre/**","/userAvatar/**","/preview/file/**").permitAll()
                        //小程序端api
                        .antMatchers("/client/**").permitAll()
                        //需要认证
                        .antMatchers("/client/me").authenticated()
                // 除上面外的所有请求全部需要鉴权认证
                .anyRequest().authenticated();
        http.formLogin()
//                配置认证成功处理器
//                .successHandler(successHandler)
//                配置认证失败处理器
                .failureHandler(failureHandler);
        //把token校验过滤器添加到过滤器链中
        http.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
        //告诉security如何处理异常
        http.exceptionHandling().authenticationEntryPoint(authenticationEntryPoint)
                .accessDeniedHandler(accessDeniedHandler);
        //允许跨域
        http.cors();
        return http.build();
    }

    @Resource
    private AuthenticationConfiguration authenticationConfiguration;

    @Bean
    public AuthenticationManager authenticationManager() throws Exception{
        AuthenticationManager authenticationManager = authenticationConfiguration.getAuthenticationManager();
        return authenticationManager;
    }

}
